package operator;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import io.rong.RongCloud;
import io.rong.models.TokenReslut;

public class Login extends HttpServlet {
	String response = "";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		String user_id = req.getParameter("id");
		String pwd = req.getParameter("pwd");
		String user_token = "";
		String appKey = "8luwapkvu83al";// 替换成您的appkey
		String appSecret = "g81fRa6ndbvz";// 替换成匹配上面key的secret

		Reader reader = null;
		RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
		System.out.println("************************User********************");
		System.out.println("user_id-->" + user_id);
		System.out.println("pwd-->" + pwd);
		// 获取 Token 方法
		TokenReslut userGetTokenResult;
		try {
			userGetTokenResult = rongCloud.user.getToken(user_id, "", "http://www.rongcloud.cn/images/logo.png");
			user_token = userGetTokenResult.toString().substring(21, userGetTokenResult.toString().length() - 17);
			System.out.println("getToken:  " + user_token);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*
		 * 此处至66行，是连接mysql数据库的操作
		 */
		// 驱动程序名
		String driver = "com.mysql.jdbc.Driver";
		// URL指向要访问的数据库名world
		String url = "jdbc:mysql://127.0.0.1:3306/FunChat";
		// MySQL配置时的用户名
		String user = "root";
		// MySQL配置时的密码
		String password = "123";
		int i = 0;
		try {
			// 加载驱动程序
			Class.forName(driver);
			// 连续数据库
			Connection conn = (Connection) DriverManager.getConnection(url, user, password);
			if (!conn.isClosed())
				System.out.println("Succeeded connecting to the Database!");
			// statement用来执行SQL语句
			Statement statement = (Statement) conn.createStatement();
			// 判断该用户是否已经注册过了
			String select_sql = "select * from Regist where user_id =" + "'" + user_id + "'";
			ResultSet rs = statement.executeQuery(select_sql);
			if (!rs.next()) {
				response = "用户不存在，请先注册!";
				System.out.println("用户不存在");
			} else {
				response = "1";
				System.out.println("允许登录");
			}
			rs.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			System.out.println("Sorry,can`t find the Driver!");
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		String jsonStr = response;
		PrintWriter out = null;
		try {
			out = resp.getWriter();
			out.write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}
}
